package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.activity.service.IFlowService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessFlag;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.FyProjectApply;
import com.ruoyi.system.domain.FyProjectApplyAncillary;
import com.ruoyi.system.domain.WcContentExamine;
import com.ruoyi.system.domain.WcContentExamineAncillary;
import com.ruoyi.system.domain.vo.business.*;
import com.ruoyi.system.mapper.WcContentExamineMapper;
import com.ruoyi.system.service.IWcContentExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 影片内容审查Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@Service
public class WcContentExamineServiceImpl extends ServiceImpl<WcContentExamineMapper, WcContentExamine> implements IWcContentExamineService
{
    @Autowired
    private WcContentExamineMapper wcContentExamineMapper;
    @Autowired
    private WcContentExamineAncillaryServiceImpl ancillaryService;
    @Autowired
    private IFlowService flowServiceImpl;

    private final static String SL = "受理";
    private final static String ZJPS = "专家评审";
    private final static String SCJD = "审查决定";
    private final static String GSFZ = "公示发证";

    /**
     * 查询影片内容审查
     *
     * @param id 影片内容审查主键
     * @return 影片内容审查
     */
    @Override
    public WcContentExamine selectWcContentExamineById(Long id)
    {
        return wcContentExamineMapper.selectWcContentExamineById(id);
    }

    /**
     * 查询影片内容审查列表
     *
     * @param wcContentExamine 影片内容审查
     * @return 影片内容审查
     */
    @Override
    public List<WcContentExamine> selectWcContentExamineList(WcContentExamine wcContentExamine)
    {
        return wcContentExamineMapper.selectWcContentExamineList(wcContentExamine);
    }

    /**
     * 新增影片内容审查
     *
     * @param wcContentExamine 影片内容审查
     * @return 结果
     */
    @Override
    public int insertWcContentExamine(WcContentExamine wcContentExamine)
    {
        wcContentExamine.setCreateTime(DateUtils.getNowLocalDateTime());
        return wcContentExamineMapper.insertWcContentExamine(wcContentExamine);
    }

    /**
     * 修改影片内容审查
     *
     * @param wcContentExamine 影片内容审查
     * @return 结果
     */
    @Override
    public int updateWcContentExamine(WcContentExamine wcContentExamine)
    {
        wcContentExamine.setUpdateTime(DateUtils.getNowLocalDateTime());
        return wcContentExamineMapper.updateWcContentExamine(wcContentExamine);
    }

    /**
     * 批量删除影片内容审查
     *
     * @param ids 需要删除的影片内容审查主键
     * @return 结果
     */
    @Override
    public int deleteWcContentExamineByIds(String ids)
    {
        return wcContentExamineMapper.deleteWcContentExamineByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除剧本备案申请信息
     *
     * @param id 剧本备案申请主键
     * @return 结果
     */
    @Override
    public int deleteWcContentExamineById(Long id)
    {
        return wcContentExamineMapper.deleteWcContentExamineById(id);
    }

    @Override
    public Page<WcContentExamineSelectListVO> getList(WcContentExamineSelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<WcContentExamineSelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return wcContentExamineMapper.getList(page, dto);
    }

    @Override
    public WcContentExamineDetailVO selectWcContentExamineDetailVOById(Long id) {
        return wcContentExamineMapper.selectWcContentExamineDetailVOById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateSave(WcContentExamineSaveVO saveVO, Long id) throws Exception {
        if (StringUtils.isEmpty(saveVO.getWcContentExamine().getFlowId())){
            saveVO.getWcContentExamine().setFlowId(null);
        }
        WcContentExamine entity = new WcContentExamine();
        BeanUtils.copyBeanProp(entity, saveVO.getWcContentExamine());
        if (id == null || id == 0) {
            entity.setFlowId(null);
            entity.setId(null);
            this.save(entity);
            if ("0".equals(saveVO.getWcContentExamine().getIsOutbox())) {
                // 非草稿箱
                flowServiceImpl.start(entity.getId(), BusinessFlag.WC_CONTENT_EXAMINE.getValue());
            }
        }else {
            String isOutboxOld = this.getById(id).getIsOutbox();
            if ("1".equals(saveVO.getWcContentExamine().getIsOutbox())) {
                entity.setFlowId(null);
            }
            this.updateById(entity);
            if ("1".equals(isOutboxOld) && "0".equals(saveVO.getWcContentExamine().getIsOutbox())) {
                // 之前为草稿箱 保存为非草稿箱
                String processInstanceId = flowServiceImpl.start(entity.getId(), BusinessFlag.WC_CONTENT_EXAMINE.getValue());
                entity.setFlowId(processInstanceId);
            }

        }

        WcContentExamineAncillary ancillary = new WcContentExamineAncillary();
        BeanUtils.copyBeanProp(ancillary, saveVO);
        ancillary.setWcContentExamineId(entity.getId());
        if (id == null || id == 0) {
            ancillaryService.save(ancillary);
        }else {
            UpdateWrapper<WcContentExamineAncillary> wrapper = new UpdateWrapper<>();
            wrapper.eq("wc_content_examine_id",ancillary.getWcContentExamineId());
            ancillaryService.saveOrUpdate(ancillary,wrapper);
        }

    }

    /**
     * 查询影片内容审查--受理列表
     * @param dto
     * @return
     */
    @Override
    public Page<WcContentExamineSelectListVO> acceptanceList(WcContentExamineSelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<WcContentExamineSelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return wcContentExamineMapper.getAcceptanceList(page, dto, SL);
    }

    /**
     * 查询影片内容审查--【专家评审】列表
     * @param dto
     * @return
     */
    @Override
    public Page<WcContentExamineSelectListVO> expertReviewList(WcContentExamineSelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<WcContentExamineSelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return wcContentExamineMapper.getAcceptanceList(page, dto, ZJPS);
    }

    /**
     * 查询影片内容审查--【公示发证】列表
     * @param dto
     * @return
     */
    @Override
    public Page<WcContentExamineSelectListVO> publicizeCertificationList(WcContentExamineSelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<WcContentExamineSelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return wcContentExamineMapper.getAcceptanceList(page, dto, GSFZ);
    }

    /**
     * 查询影片内容审查--【审查决定】列表
     * @param dto
     * @return
     */
    @Override
    public Page<WcContentExamineSelectListVO> reviewDecisionList(WcContentExamineSelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<WcContentExamineSelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return wcContentExamineMapper.getAcceptanceList(page, dto, SCJD);
    }

    /**
     * 影片内容审查--查询影片内容审查【公示信息】列表
     * @param dto
     * @return
     */
    @Override
    public Page<WcContentExaminePublicityInfoListVO> publicityInfoList(WcContentExaminePublicityInfoListDto dto) {
//        Long userId = SecurityUtils.getUserId();
//        dto.setApprovalUserId(userId);
        Page<WcContentExaminePublicityInfoListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return wcContentExamineMapper.getPublicityInfoList(page, dto);
    }
}
